package com.ssms.controller;

import com.ssms.pojo.CommonResult;
import com.ssms.pojo.Employee;
import com.ssms.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rui
 * @version 1.0
 * @date 2020/12/28 11:17
 */
@Slf4j
@RestController
@Api("员工管理")
@CrossOrigin(allowCredentials="true",maxAge = 3600)
@RequestMapping("/manager/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("添加员工")
    @PostMapping("/addEmp")
    public CommonResult<Employee> addEmp(@RequestBody Employee employee){
        try {
            employeeService.addEmp(employee);
            return new CommonResult<>(200, "添加成功", employee);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "添加失败");
        }
    }

    @ApiOperation("删除员工")
    @PostMapping("/delEmp/{id}")
    public CommonResult<Employee> delEmp(@PathVariable("id") int id){
        try {
            employeeService.delEmp(id);
            return new CommonResult<>(200, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "删除失败");
        }
    }

    @ApiOperation("更新员工")
    @PostMapping("/updateEmp")
    public CommonResult<Employee> updateEmp(@RequestBody Employee employee){
        int i = employeeService.updateEmp(employee);
        if (i > 0){
            return new CommonResult<>(200, "更新成功");
        }else {
            return new CommonResult<>(400, "更新失败");
        }
    }

    @ApiOperation("根据id查询员工")
    @PostMapping("/findEmpById/{id}")
    public CommonResult<Employee> findEmpById(@PathVariable("id") int id){
        try {
            Employee emp = employeeService.findEmpById(id);
            return new CommonResult<>(200, "查询成功！", emp);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "查询失败");
        }
    }

    @ApiOperation("查询所有员工")
    @GetMapping("/findAllEmps")
    public List<Employee> findAllEmps(){
        List<Employee> list = employeeService.findAllEmps();
        return list;
    }

}
